package likou;

/**
 * @author: Tangxz
 * @email: 1171702529@qq.com
 * @cate: 2020/11/29 08:37
 */
public class _976 {
    public static void main(String[] args) {
        System.out.println(largestPerimeter(new int[]{2,1,2}));
    }

    public static int largestPerimeter(int[] A) {
        quickSort(A, 0, A.length - 1);
        int[] daan = new int[3];
        int now = 0;
        for (int i = A.length - 1; i >= 0; i--) {
            daan[now] = A[i];
            now++;
            if (now == 3) {
                if (daan[0] >= daan[1] + daan[2]) {
                    daan[0] = daan[1];
                    daan[1] = daan[2];
                    now--;
                } else {
                    break;
                }
            }
        }
        return now==3?daan[0] + daan[1] + daan[2]:0;
    }

    public static void quickSort(int[] arr, int startIndex, int endIndex) {
        if (startIndex >= endIndex) {
            return;
        }
        int prvotIndex = partition(arr, startIndex, endIndex);

        quickSort(arr, startIndex, prvotIndex - 1);
        quickSort(arr, prvotIndex + 1, endIndex);
    }

    public static int partition(int[] arr, int startIndex, int endIndex) {
        int pivot = arr[startIndex];
        int left = startIndex;
        int right = endIndex;
        while (left != right) {
            while (left < right && arr[right] > pivot) {
                right--;
            }
            while (left < right && arr[left] <= pivot) {
                left++;
            }
            if (left < right) {
                int p = arr[left];
                arr[left] = arr[right];
                arr[right] = p;
            }
        }
        arr[startIndex] = arr[left];
        arr[left] = pivot;
        return left;
    }
}
